package cn.edu.zafu.bigdata.linfeifei;

public class TestStaticDemo {
    public static void main(String[] args) {
        // 创建多个 StaticDemo 类的实例
        StaticDemo obj1 = new StaticDemo();
        StaticDemo obj2 = new StaticDemo();
        StaticDemo obj3 = new StaticDemo();

        // 调用 StaticDemo.displayCount() 静态方法，验证实例创建次数
        StaticDemo.displayCount();

        // 观察程序的执行顺序
    }

    public static class StaticDemo {
        // 静态变量，用于计数类的实例创建次数
        static int count = 0;
        // 实例变量，用于存储每个实例的唯一编号
        int id;

        // 静态初始化块
        static {
            System.out.println("Static block executed");
            count = 0;
        }

        // 实例初始化块
        {
            System.out.println("Instance block executed");
            id = count;
            count++;
        }

        // 构造函数
        public StaticDemo() {
            System.out.println("Constructor executed for ID: " + id);
        }

        // 静态方法，用于打印当前创建了多少个实例
        public static void displayCount() {
            System.out.println("Total instances created: " + count);
        }
    }
}

